package utils ;

import javax.servlet.http.Cookie ;
import javax.servlet.http.HttpServletRequest ;
import javax.servlet.http.HttpServletResponse ;

import org.springframework.util.StringUtils ;

/**
 * @author 80002165 @date 2017年7月5日 上午9:55:56
 */
public class CookieUtil {
    
    private static final String SECRET_COOKIE_KEY = "cookieKEY" ;
    
    /**
     * 创建cookie，key-value不可为null
     * @author 80002165 @date 2017年7月5日 下午5:17:45
     * @param cookeName
     * @param cookieValue
     * @return
     */
    public static Cookie createCookie(String cookeName, String cookieValue) {
        if (StringUtils.hasText(cookieValue) && StringUtils.hasText(cookieValue)) {
            Cookie cookie = new Cookie(cookeName, cookieValue) ;
            return cookie ;
        }
        return null ;
    }
    
    public static Cookie createSecretCookie(String cookeName, String cookieValue, String key) {
        if (!StringUtils.hasText(key))
            key = SECRET_COOKIE_KEY ;
        
        if (StringUtils.hasText(cookieValue) && StringUtils.hasText(cookieValue)) {
            cookieValue = MD5Util.MD5(cookieValue + key) ;
            Cookie cookie = new Cookie(cookeName, cookieValue) ;
            return cookie ;
        }
        return null ;
    }
    
    public static boolean checkSecretCookie(HttpServletRequest request, String cookieName, String cookieValue) {
        String cookieVal = getCookieValue(request, cookieName) ;
        cookieVal = MD5Util.MD5(cookieVal + SECRET_COOKIE_KEY) ;
        return cookieValue.equals(cookieVal) ;
    }
    
    public static Cookie setCookie(HttpServletResponse response, String cookeName, String cookieValue) {
        if (StringUtils.hasText(cookieValue)) {
            Cookie cookie = createCookie(cookeName, cookieValue) ;
            response.addCookie(cookie) ;
            return cookie ;
        }
        return null ;
    }
    
    public static Cookie setSecretCookie(HttpServletResponse response, String cookeName, String cookieValue) {
        if (StringUtils.hasText(cookieValue)) {
            Cookie cookie = createSecretCookie(cookeName, cookieValue, SECRET_COOKIE_KEY) ;
            response.addCookie(cookie) ;
            return cookie ;
        }
        return null ;
    }
    
    public static void setCookie(HttpServletResponse response, String cookeName, String cookieValue, int maxAge) {
        setCookie(response, cookeName, cookieValue, maxAge, "/") ;
    }
    
    public static void setCookie(HttpServletResponse response, String cookeName, String cookieValue, int maxAge, String path) {
        Cookie cookie = setCookie(response, cookeName, cookieValue) ;
        if (cookie != null) {
            cookie.setMaxAge(maxAge) ;
            cookie.setPath(path) ;
        }
    }
    
    /**
     * 查询cookie值
     * @author 80002165 @date 2017年7月5日 上午10:08:53
     * @param request
     * @param cookieName
     * @return
     */
    public static String getCookieValue(HttpServletRequest request, String cookieName) {
        if (StringUtils.hasText(cookieName)) {
            Cookie[] cookies = request.getCookies() ;
            if (cookies == null)
                return null ;
            for (Cookie cookie : cookies) {
                if (cookieName.equals(cookie.getName())) {
                    return cookie.getValue() ;
                }
            }
        }
        return null ;
    }
    
    /**
     * 清空cookie值
     * @author 80002165 @date 2017年7月5日 上午10:08:39
     * @param request
     * @param response
     * @param cookieName
     */
    public static void removeCookieValue(HttpServletRequest request, HttpServletResponse response, String cookieName) {
        Cookie[] cookies = request.getCookies() ;
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals(cookieName)) {
                    Cookie newCookie = new Cookie(cookieName, null) ;
                    response.addCookie(newCookie) ;
                    break ;
                }
            }
        }
    }
}
